package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyDataPersistence;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring定时器,完成redis的数据落地/持久化/固化
 */
//@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IStrategyStatisVOService statisVOService;


    //@Scheduled(cron = "0/5 * * * * ?")//每隔5秒执行一次
    public void redisDataPersistence(){
        System.out.println("redis数据落地开始-------------------------");
        //从redis中获取所有的vo对象
        List<StrategyStatisVO> list = statisVOService.listStatisVO(RedisKeys.STRATEGY_STATIS_VO.join("*"));
        StrategyDataPersistence pvo = null;
        for (StrategyStatisVO vo : list) {
            //将redis中的vo对象数据copy到新建的类中,为了解决彼此依赖容易出错
            pvo = new StrategyDataPersistence();
            BeanUtil.copyProperties(pvo,vo);
            //将vo对象update到mysql数据库中
            detailService.updateStatisVO(pvo);
        }
        System.out.println("redis数据落地结束-------------------------");
    }
}
